import java.util.HashSet;

/**
 * @author wangj881
 * @date 2025-01-09 17:09
 */
public class L142_3 {

    public class Solution {
        public ListNode detectCycle(ListNode head) {
            HashSet<ListNode> nodes = new HashSet<>();
            while (head != null) {
                boolean add = nodes.add(head);
                if (!add) {
                    return head;
                }
                head = head.next;
            }
            return null;
        }
    }
}
